<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input
        placeholder="用户名手机"
        @focus="handErrMsg('username')"
        v-model="form.username"
      >
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input
        placeholder="验证码"
        @focus="handErrMsg('captcha')"
        v-model="form.captcha"
      >
        <template slot="append">
          <el-button @click="handleSendCaptcha"> 发送验证码 </el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input
        placeholder="你的名字"
        @focus="handErrMsg('nickname')"
        v-model="form.nickname"
      >
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        @focus="handErrMsg('password')"
        v-model="form.password"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input
        placeholder="确认密码"
        type="password"
        @focus="handErrMsg('checkPassword')"
        v-model="form.checkPassword"
      >
      </el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">
      注册
    </el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    //  使用函数校验
    const validatorPass = (rule, value, callback) => {
      // rule:验证的哪条规则, value：输入框的值, callback回调放行，可以使用new Error('提示的错误')提示错误
      if (value == "") {
        //二次密码 输入为空 提示错误
        callback(new Error("请输入密码"));
      } else if (value != this.form.password) {
        //验证输入的密码与之前设置的密码不一致  提示错误
        callback(new Error("验证密码与设置的密码不一致"));
      } else {
        // 放行
        callback();
      }
    };
    return {
      form: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPassword: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
            trigger: "blur",
          },
          {
            // 正则判断手机号
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入11位的手机号",
            trigger: "blur",
          },
        ],

        nickname: [
          {
            required: true,
            message: "昵称不能为空",
            trigger: "blur",
          },
          {
            min: 6,
            max: 11,
            message: "请输入6到11位的昵称",
            trigger: "blur",
          },
        ],
        captcha: [
          {
            required: true,
            message: "验证码不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
          {
            // 6到11位的数字
            // type: "number",
            min: 6,
            max: 11,
            message: "请输入6到11位的数字",
            trigger: "blur",
          },
        ],
        checkPassword: [
          {
            validator: validatorPass,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handErrMsg(prop) {
      // 处理输入框聚焦清空校验提示
      this.$refs.form.clearValidate(prop);
    },
    // 验证码
    handleSendCaptcha() {
      // 发送请求
      this.$axios({
        method: "post",
        url: "/captchas",
        data: { tel: this.form.username },
      }).then((res) => {
        // console.log(res);
        this.$message.success("成功获取当前手机的验证码：" + res.data.code);
      });
    },
    // 注册
    handleRegSubmit() {
      // 发送请求注册前需要进行总校验 使用promise方法处理
      this.$refs.form
        .validate()
        .then((res) => {
          // console.log(res);
          // 校验成功后发送请求注册
          if (res) {
            // 改造form的数据 满足发送请求参数的字段
            const { checkPassword, ...data } = this.form;
            // 发送请求注册
            this.$axios({
              method: "post",
              url: "/accounts/register",
              data,
            }).then((res) => {
              console.log(res);
              // 提示注册成功
              this.$message.success("注册成功");
              //刷新 个人信息
              this.$store.commit("user/setUserInfo", res.data);
              //  切换 登录
              // 主动发送事件到父组件接收
              this.$emit("ToLogin");
            });
          }
        })
        .catch((err) => {
          console.log(err);
          // 提示校验失败
          this.$message.error("校验失败,请重新输入");
        });
    },
  },
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>